
#include <assert.h>
#include "app_platform/cmd_obj.h"
#include "app_platform/cmd_obj_ext.h"
#include "app_platform/cntl_info.h"
#include "app_platform/intf_service_control.h"
#include "gismapgatewayao_adaptor.h"
#include "gismapgatewayao_req_resp.h"


std::string g_sgismapgatewayaoStandAlone;

///////////////////////
// class CGisMapGatewayAoSvcFactory
int CGisMapGatewayAoSvcFactory::init(void* pvParam)
{
	IUserDefineCntl* pCntl = reinterpret_cast<IUserDefineCntl*>(pvParam);
	assert(pCntl);
    pCntl->getConfig("StandAlone", g_sgismapgatewayaoStandAlone);
         

	return CGisMapGatewayAo::Initialize(pCntl);
}

int CGisMapGatewayAoSvcFactory::CXYZTimerHandler::OnTimer(int iTimerId)
{
    assert(0);
	return 0;
}

int CGisMapGatewayAoSvcFactory::fini(void* pvParam)
{
    return 0;
}

uint32_t CGisMapGatewayAoSvcFactory::getCateId() const
{   
	//方法已废弃
    return 0;
}

IService* CGisMapGatewayAoSvcFactory::createService(uint32_t dwcmdId)
{
    return (new CGisMapGatewayAoService(dwcmdId));
}

void CGisMapGatewayAoSvcFactory::getServiceType(ServiceTypeMap_T& mapSvcType)
{
	mapSvcType[GISMAPGATEWAYAO_GETALIYUNCOORDINATES_REQ_CMD_ID] = true;
	mapSvcType[GISMAPGATEWAYAO_GETBAIDUCOORDINATES_REQ_CMD_ID] = true;
	mapSvcType[GISMAPGATEWAYAO_GETGAODECOORDINATES_REQ_CMD_ID] = true;
	mapSvcType[GISMAPGATEWAYAO_GETGOOGLECOORDINATES_REQ_CMD_ID] = true;
	mapSvcType[GISMAPGATEWAYAO_GETSOSO2COORDINATES_REQ_CMD_ID] = true;
	mapSvcType[GISMAPGATEWAYAO_GETSOSOCOORDINATES_REQ_CMD_ID] = true;
}

////////////////////////
// class CGisMapGatewayAoService
CGisMapGatewayAoService::CGisMapGatewayAoService(uint32_t dwcmdId) 
: m_dwcmdId(dwcmdId)
{
    enum DeployMethod method = DEPLOY_DETACH;
    if(!g_sgismapgatewayaoStandAlone.empty() && g_sgismapgatewayaoStandAlone != "0") // Stand-Alone
        method = DEPLOY_STANDALONE;
        
    m_pSvc = new CGisMapGatewayAo(method);
}

void CGisMapGatewayAoService::Reset() 
{ 
	m_pSvc -> Reset();
}
    
void CGisMapGatewayAoService::getCmdIdRange(uint32_t& dwcmdIdBegin, uint32_t& dwcmdIdEnd) const 
{ 
    dwcmdIdBegin = m_dwcmdId;
    dwcmdIdEnd = m_dwcmdId;
} 
	
int CGisMapGatewayAoService::Execute(CCmdObj& oCmd)
{
	switch(oCmd.getCmdId())
	{
	case GISMAPGATEWAYAO_GETALIYUNCOORDINATES_REQ_CMD_ID:
        return GetAliyunCoordinates(oCmd);
	case GISMAPGATEWAYAO_GETBAIDUCOORDINATES_REQ_CMD_ID:
        return GetBaiduCoordinates(oCmd);
	case GISMAPGATEWAYAO_GETGAODECOORDINATES_REQ_CMD_ID:
        return GetGaodeCoordinates(oCmd);
	case GISMAPGATEWAYAO_GETGOOGLECOORDINATES_REQ_CMD_ID:
        return GetGoogleCoordinates(oCmd);
	case GISMAPGATEWAYAO_GETSOSO2COORDINATES_REQ_CMD_ID:
        return GetSoso2Coordinates(oCmd);
	case GISMAPGATEWAYAO_GETSOSOCOORDINATES_REQ_CMD_ID:
        return GetSosoCoordinates(oCmd);
	default:
		assert(0);
	}
	return 0;
}

int CGisMapGatewayAoService::GetAliyunCoordinates(CCmdObj& oCmd)
{
	char* pBuf = NULL; uint32_t nLen = 0;
    oCmd.getBuffer(pBuf, nLen);
    CByteStreamNetwork bs(pBuf, nLen);
    bs.isStoring(false);
    bs.setVersion(oCmd.getVersion());
    CGisMapGatewayAoGetAliyunCoordinatesReq oReq;
    if(!oReq.Serialize(bs))
    {
        m_sLastErrMsg = "Req.Serialize(bs) failed.";
        return -1;
    }

    CCntlInfo oCntlInfo;
    oCntlInfo.setCntl(reinterpret_cast<void*>(m_pSvcCntl));
    oCntlInfo.setOperatorUin(oCmd.getOperatorUin());
	oCntlInfo.setOperatorClientIP(oCmd.getOperatorClientIP());
	oCntlInfo.setOperatorKey(oCmd.getOperatorKey());
    oCntlInfo.setRouteKey(oCmd.getUin());
    oCntlInfo.setCmdId(oCmd.getCmdId());
    oCntlInfo.setNetioClientIP(oCmd.getNetioClientIP());
	oCntlInfo.setSerialNo(oCmd.getSerialNo());
	oCntlInfo.setPkgTimestamp(oCmd.getPkgTimestamp());
	
	if (oCmd.getVersion() == 3)
    {
        CCmdObjExt& oCmdExt = dynamic_cast<CCmdObjExt&>(oCmd);
        oCntlInfo.setAppId(oCmdExt.getReserve1());
        oCntlInfo.setDomainId(oCmdExt.getReserve2());
    }
	
	CGisMapGatewayAoGetAliyunCoordinatesResp oResp;
	
    oResp.dwResult = m_pSvc->GetAliyunCoordinates(
		oCntlInfo,
		oReq.machineKey,
		oReq.source,
		oReq.sceneId,
		oReq.coordinatesQueryPo,
		oReq.inReserve,
		oReq.coordinatesPo,
		oReq.errMsg,
		oReq.outReserve
    );
	oResp.coordinatesPo = oReq.coordinatesPo;
	oResp.errMsg = oReq.errMsg;
	oResp.outReserve = oReq.outReserve;

    return m_pSvcCntl->putSvcObj(&oResp, oResp.dwResult);
}


int CGisMapGatewayAoService::GetBaiduCoordinates(CCmdObj& oCmd)
{
	char* pBuf = NULL; uint32_t nLen = 0;
    oCmd.getBuffer(pBuf, nLen);
    CByteStreamNetwork bs(pBuf, nLen);
    bs.isStoring(false);
    bs.setVersion(oCmd.getVersion());
    CGisMapGatewayAoGetBaiduCoordinatesReq oReq;
    if(!oReq.Serialize(bs))
    {
        m_sLastErrMsg = "Req.Serialize(bs) failed.";
        return -1;
    }

    CCntlInfo oCntlInfo;
    oCntlInfo.setCntl(reinterpret_cast<void*>(m_pSvcCntl));
    oCntlInfo.setOperatorUin(oCmd.getOperatorUin());
	oCntlInfo.setOperatorClientIP(oCmd.getOperatorClientIP());
	oCntlInfo.setOperatorKey(oCmd.getOperatorKey());
    oCntlInfo.setRouteKey(oCmd.getUin());
    oCntlInfo.setCmdId(oCmd.getCmdId());
    oCntlInfo.setNetioClientIP(oCmd.getNetioClientIP());
	oCntlInfo.setSerialNo(oCmd.getSerialNo());
	oCntlInfo.setPkgTimestamp(oCmd.getPkgTimestamp());
	
	if (oCmd.getVersion() == 3)
    {
        CCmdObjExt& oCmdExt = dynamic_cast<CCmdObjExt&>(oCmd);
        oCntlInfo.setAppId(oCmdExt.getReserve1());
        oCntlInfo.setDomainId(oCmdExt.getReserve2());
    }
	
	CGisMapGatewayAoGetBaiduCoordinatesResp oResp;
	
    oResp.dwResult = m_pSvc->GetBaiduCoordinates(
		oCntlInfo,
		oReq.machineKey,
		oReq.source,
		oReq.sceneId,
		oReq.coordinatesQueryPo,
		oReq.inReserve,
		oReq.coordinatesPo,
		oReq.errMsg,
		oReq.outReserve
    );
	oResp.coordinatesPo = oReq.coordinatesPo;
	oResp.errMsg = oReq.errMsg;
	oResp.outReserve = oReq.outReserve;

    return m_pSvcCntl->putSvcObj(&oResp, oResp.dwResult);
}


int CGisMapGatewayAoService::GetGaodeCoordinates(CCmdObj& oCmd)
{
	char* pBuf = NULL; uint32_t nLen = 0;
    oCmd.getBuffer(pBuf, nLen);
    CByteStreamNetwork bs(pBuf, nLen);
    bs.isStoring(false);
    bs.setVersion(oCmd.getVersion());
    CGisMapGatewayAoGetGaodeCoordinatesReq oReq;
    if(!oReq.Serialize(bs))
    {
        m_sLastErrMsg = "Req.Serialize(bs) failed.";
        return -1;
    }

    CCntlInfo oCntlInfo;
    oCntlInfo.setCntl(reinterpret_cast<void*>(m_pSvcCntl));
    oCntlInfo.setOperatorUin(oCmd.getOperatorUin());
	oCntlInfo.setOperatorClientIP(oCmd.getOperatorClientIP());
	oCntlInfo.setOperatorKey(oCmd.getOperatorKey());
    oCntlInfo.setRouteKey(oCmd.getUin());
    oCntlInfo.setCmdId(oCmd.getCmdId());
    oCntlInfo.setNetioClientIP(oCmd.getNetioClientIP());
	oCntlInfo.setSerialNo(oCmd.getSerialNo());
	oCntlInfo.setPkgTimestamp(oCmd.getPkgTimestamp());
	
	if (oCmd.getVersion() == 3)
    {
        CCmdObjExt& oCmdExt = dynamic_cast<CCmdObjExt&>(oCmd);
        oCntlInfo.setAppId(oCmdExt.getReserve1());
        oCntlInfo.setDomainId(oCmdExt.getReserve2());
    }
	
	CGisMapGatewayAoGetGaodeCoordinatesResp oResp;
	
    oResp.dwResult = m_pSvc->GetGaodeCoordinates(
		oCntlInfo,
		oReq.machineKey,
		oReq.source,
		oReq.sceneId,
		oReq.coordinatesQueryPo,
		oReq.inReserve,
		oReq.coordinatesPo,
		oReq.errMsg,
		oReq.outReserve
    );
	oResp.coordinatesPo = oReq.coordinatesPo;
	oResp.errMsg = oReq.errMsg;
	oResp.outReserve = oReq.outReserve;

    return m_pSvcCntl->putSvcObj(&oResp, oResp.dwResult);
}


int CGisMapGatewayAoService::GetGoogleCoordinates(CCmdObj& oCmd)
{
	char* pBuf = NULL; uint32_t nLen = 0;
    oCmd.getBuffer(pBuf, nLen);
    CByteStreamNetwork bs(pBuf, nLen);
    bs.isStoring(false);
    bs.setVersion(oCmd.getVersion());
    CGisMapGatewayAoGetGoogleCoordinatesReq oReq;
    if(!oReq.Serialize(bs))
    {
        m_sLastErrMsg = "Req.Serialize(bs) failed.";
        return -1;
    }

    CCntlInfo oCntlInfo;
    oCntlInfo.setCntl(reinterpret_cast<void*>(m_pSvcCntl));
    oCntlInfo.setOperatorUin(oCmd.getOperatorUin());
	oCntlInfo.setOperatorClientIP(oCmd.getOperatorClientIP());
	oCntlInfo.setOperatorKey(oCmd.getOperatorKey());
    oCntlInfo.setRouteKey(oCmd.getUin());
    oCntlInfo.setCmdId(oCmd.getCmdId());
    oCntlInfo.setNetioClientIP(oCmd.getNetioClientIP());
	oCntlInfo.setSerialNo(oCmd.getSerialNo());
	oCntlInfo.setPkgTimestamp(oCmd.getPkgTimestamp());
	
	if (oCmd.getVersion() == 3)
    {
        CCmdObjExt& oCmdExt = dynamic_cast<CCmdObjExt&>(oCmd);
        oCntlInfo.setAppId(oCmdExt.getReserve1());
        oCntlInfo.setDomainId(oCmdExt.getReserve2());
    }
	
	CGisMapGatewayAoGetGoogleCoordinatesResp oResp;
	
    oResp.dwResult = m_pSvc->GetGoogleCoordinates(
		oCntlInfo,
		oReq.machineKey,
		oReq.source,
		oReq.sceneId,
		oReq.coordinatesQueryPo,
		oReq.inReserve,
		oReq.coordinatesPo,
		oReq.errMsg,
		oReq.outReserve
    );
	oResp.coordinatesPo = oReq.coordinatesPo;
	oResp.errMsg = oReq.errMsg;
	oResp.outReserve = oReq.outReserve;

    return m_pSvcCntl->putSvcObj(&oResp, oResp.dwResult);
}


int CGisMapGatewayAoService::GetSoso2Coordinates(CCmdObj& oCmd)
{
	char* pBuf = NULL; uint32_t nLen = 0;
    oCmd.getBuffer(pBuf, nLen);
    CByteStreamNetwork bs(pBuf, nLen);
    bs.isStoring(false);
    bs.setVersion(oCmd.getVersion());
    CGisMapGatewayAoGetSoso2CoordinatesReq oReq;
    if(!oReq.Serialize(bs))
    {
        m_sLastErrMsg = "Req.Serialize(bs) failed.";
        return -1;
    }

    CCntlInfo oCntlInfo;
    oCntlInfo.setCntl(reinterpret_cast<void*>(m_pSvcCntl));
    oCntlInfo.setOperatorUin(oCmd.getOperatorUin());
	oCntlInfo.setOperatorClientIP(oCmd.getOperatorClientIP());
	oCntlInfo.setOperatorKey(oCmd.getOperatorKey());
    oCntlInfo.setRouteKey(oCmd.getUin());
    oCntlInfo.setCmdId(oCmd.getCmdId());
    oCntlInfo.setNetioClientIP(oCmd.getNetioClientIP());
	oCntlInfo.setSerialNo(oCmd.getSerialNo());
	oCntlInfo.setPkgTimestamp(oCmd.getPkgTimestamp());
	
	if (oCmd.getVersion() == 3)
    {
        CCmdObjExt& oCmdExt = dynamic_cast<CCmdObjExt&>(oCmd);
        oCntlInfo.setAppId(oCmdExt.getReserve1());
        oCntlInfo.setDomainId(oCmdExt.getReserve2());
    }
	
	CGisMapGatewayAoGetSoso2CoordinatesResp oResp;
	
    oResp.dwResult = m_pSvc->GetSoso2Coordinates(
		oCntlInfo,
		oReq.machineKey,
		oReq.source,
		oReq.sceneId,
		oReq.coordinatesQueryPo,
		oReq.inReserve,
		oReq.coordinatesPo,
		oReq.errMsg,
		oReq.outReserve
    );
	oResp.coordinatesPo = oReq.coordinatesPo;
	oResp.errMsg = oReq.errMsg;
	oResp.outReserve = oReq.outReserve;

    return m_pSvcCntl->putSvcObj(&oResp, oResp.dwResult);
}


int CGisMapGatewayAoService::GetSosoCoordinates(CCmdObj& oCmd)
{
	char* pBuf = NULL; uint32_t nLen = 0;
    oCmd.getBuffer(pBuf, nLen);
    CByteStreamNetwork bs(pBuf, nLen);
    bs.isStoring(false);
    bs.setVersion(oCmd.getVersion());
    CGisMapGatewayAoGetSosoCoordinatesReq oReq;
    if(!oReq.Serialize(bs))
    {
        m_sLastErrMsg = "Req.Serialize(bs) failed.";
        return -1;
    }

    CCntlInfo oCntlInfo;
    oCntlInfo.setCntl(reinterpret_cast<void*>(m_pSvcCntl));
    oCntlInfo.setOperatorUin(oCmd.getOperatorUin());
	oCntlInfo.setOperatorClientIP(oCmd.getOperatorClientIP());
	oCntlInfo.setOperatorKey(oCmd.getOperatorKey());
    oCntlInfo.setRouteKey(oCmd.getUin());
    oCntlInfo.setCmdId(oCmd.getCmdId());
    oCntlInfo.setNetioClientIP(oCmd.getNetioClientIP());
	oCntlInfo.setSerialNo(oCmd.getSerialNo());
	oCntlInfo.setPkgTimestamp(oCmd.getPkgTimestamp());
	
	if (oCmd.getVersion() == 3)
    {
        CCmdObjExt& oCmdExt = dynamic_cast<CCmdObjExt&>(oCmd);
        oCntlInfo.setAppId(oCmdExt.getReserve1());
        oCntlInfo.setDomainId(oCmdExt.getReserve2());
    }
	
	CGisMapGatewayAoGetSosoCoordinatesResp oResp;
	
    oResp.dwResult = m_pSvc->GetSosoCoordinates(
		oCntlInfo,
		oReq.machineKey,
		oReq.source,
		oReq.sceneId,
		oReq.coordinatesQueryPo,
		oReq.inReserve,
		oReq.coordinatesPo,
		oReq.errMsg,
		oReq.outReserve
    );
	oResp.coordinatesPo = oReq.coordinatesPo;
	oResp.errMsg = oReq.errMsg;
	oResp.outReserve = oReq.outReserve;

    return m_pSvcCntl->putSvcObj(&oResp, oResp.dwResult);
}


#include "svc_main.h"

IServiceFactory* GetServiceFactory(void)
{
    return CSvcFactorySingleton::Instance();
}
